package com.ruoyi.my.service.impl.courseplan;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.my.domian.courseplan.CoursePlanCategoryVO;
import com.ruoyi.my.mapper.courseplan.CoursePlanCategoryMapper;
import com.ruoyi.my.service.courseplan.CoursePlanCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CoursePlanCategoryServiceImpl implements CoursePlanCategoryService {

    @Autowired
    private CoursePlanCategoryMapper coursePlanCategoryMapper;


    @Override
    public List<CoursePlanCategoryVO> selectCoursePlanCategoryList(CoursePlanCategoryVO coursePlanCategoryVO) {
        return coursePlanCategoryMapper.selectCoursePlanCategoryList(coursePlanCategoryVO);
    }

    @Override
    public int deleteCoursePlanCategoryByIds(Long[] categoryIds) {
        return coursePlanCategoryMapper.deleteCoursePlanCategoryByIds(categoryIds);
    }

    @Override
    public int updateCoursePlanCategoryById(CoursePlanCategoryVO coursePlanCategoryVO) {
        coursePlanCategoryVO.setUpdateBy(SecurityUtils.getUserId().toString());
        return coursePlanCategoryMapper.updateCoursePlanCategoryById(coursePlanCategoryVO);
    }

    @Override
    public int addCoursePlanCategory(CoursePlanCategoryVO coursePlanCategoryVO) {
        coursePlanCategoryVO.setCreateBy(SecurityUtils.getUserId().toString());
        String categoryCode = coursePlanCategoryMapper.getLastCategoryCode();
        if(categoryCode != null){
            int num = Integer.parseInt(categoryCode)  + 1;
            // 格式化为两位，不足补零
            categoryCode = String.format("%02d",  num);
        } else {
            categoryCode = "01";
        }
        coursePlanCategoryVO.setCategoryCode(categoryCode);
        return coursePlanCategoryMapper.insertCoursePlanCategory(coursePlanCategoryVO);
    }
}
